# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: 带干扰线验证码.py
@time: 2021/9/5 13:02
'''
import os

from captcha.image import ImageCaptcha
import random


list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# 定义验证码尺寸
width, height = 170, 80

#生成一万张验证码  for循环1万次
for i in range(5):
   generator = ImageCaptcha(width=width, height=height)

   # 从list中取出4个字符
   random_str = ''.join([random.choice(list) for j in range(4)])
   # 生成验证码
   img = generator.generate_image(random_str)
   # 在验证码上加干扰点
   generator.create_noise_dots(img, '#000000', 4, 40)
   # 在验证码上加干扰线
   generator.create_noise_curve(img, '#000000')
   if os.path.exists('D:\pacong\TulingPaCong\day19验证码生成及绕过\images') is False:
       os.mkdir('D:\pacong\TulingPaCong\day19验证码生成及绕过\images')
   # 将图片保存在目录yzm文件夹下
   file_name = 'D:\pacong\TulingPaCong\day19验证码生成及绕过/images/'+random_str+'.jpg'
   img.save(file_name)